﻿
#ifndef _ABSTRACTSERVERHANDLE_H_
#define _ABSTRACTSERVERHANDEL_H_

#include <map>
#include <string>
#include <vector>
#include <QString>
#include <QMap>
#include <QDebug>
#include <QJsonDocument>
#include <QJsonObject>
#include <QJsonArray>
template<class S>
class AbstractServerHandle
{
public:
    typedef void(S::*methodPointer_t)       (QString ,QString&);
    typedef void(S::*JsonmethodPointer_t)   (QJsonDocument&,QString &);
    virtual void HandleMethodCall(QString name, QString input ,QString &output)
    {
        if(!name.isEmpty() &&methods.find(name) !=methods.end())
        {
            S* instance = dynamic_cast<S*>(this);
            (instance->*methods[name])(input, output);
        }else
        {
            qDebug()<<"don't have this function! you should buy it maybe";
        }
    }
    virtual void HandleJsonMethodCall(QString name, QJsonDocument & input  ,QString &output)
    {
        if(!name.isEmpty() &&m_mJsonMethods.find(name) !=m_mJsonMethods.end())
        {
            S* instance = dynamic_cast<S*>(this);
            (instance->*m_mJsonMethods[name])(input, output);
        }else
        {
            qDebug()<<"don't have this function! you should buy it maybe";
        }
    }

protected:
    bool bindAndAddMethod(QString name, methodPointer_t pointer)
    {
        this->methods[name] = pointer;
        return true;
    }
    bool bindAndAddJsonMethod(QString name, JsonmethodPointer_t pointer)
    {
        this->m_mJsonMethods[name] = pointer;
        return true;
    }
private:
    QMap<QString, methodPointer_t>              methods;
    QMap<QString, JsonmethodPointer_t>          m_mJsonMethods;
    bool symbolExists(const QString &name)
    {
        if (methods.find(name) != methods.end())
            return true;
        return false;
    }
};

#endif
